from pynput import keyboard
import pyautogui as pg
import threading
import time
import sys
import random
def delay(n):

    if(n==0):
        n=int(random.random()*5)+0
    for i in range(n):
        if(key_listen_thread.is_alive()):
            print(n-i)
        else:
            sys.exit()
        time.sleep(1)
def on_press(key):
    try:
        print(f'字母键 {key.char} 被按下，按q键退出')
        if(key.char == "q"):
            print(f'exit')
            sys.exit()
    except AttributeError:
        print(f'特殊键 {key} 被按下')
 
def on_release(key):
    print(f'{key} 被释放')
    if key == keyboard.Key.esc:
        # 停止监听
        return False
 
# 收集事件直到按下了 Esc 键
def listen_key():
    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()
key_listen_thread=threading.Thread(target=listen_key)
key_listen_thread.start()


delay(3)
mx1,my1=pg.position()
print(mx1,my1)

delay(3)
mx2,my2=pg.position()
print(mx2,my2)
t=0

while(1):
    pg.click(x=mx1,y=my1,clicks=2,interval=0.2)
    pg.click(x=mx2,y=my2)
    print(f"click:{t}")
    t=t+1
    delay(0)
